import CryptoJS from "crypto-js";
import dayjs from "dayjs";
function getAesString(data, key, iv) {
  //加密
  var key = CryptoJS.enc.Utf8.parse(key);
  var iv = CryptoJS.enc.Utf8.parse(iv);
  var encrypted = CryptoJS.AES.encrypt(data, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString(); //返回的是base64格式的密文
}

function getAES(data) {
  //加密
  var key = "PANJIAWEI991026PANJIAWEI991026FT"; //密钥
  var iv = "1315211890591026";
  var encrypted = getAesString(data, key, iv); //密文
  var encrypted1 = CryptoJS.enc.Utf8.parse(encrypted);
  return encrypted;
}

function checkedType(target) {
  return Object.prototype.toString.call(target).slice(8, -1);
}
function clone(target) {
  let result,
    targetType = checkedType(target);

  if (targetType === "Object") {
    result = {};
  } else if (targetType === "Array") {
    result = [];
  } else {
    return target;
  }
  for (let i in target) {
    let value = target[i];
    if (checkedType(target) === "Object") {
      i = getAES(i + "");
    }
    if (checkedType(value) === "Object" || checkedType(value) === "Array") {
      result[i] = clone(value);
    } else {
      result[i] = getAES(value + "");
    }
  }
  return result;
}
const encryption = (obj) => {
  let arr = obj;
  const date = dayjs(new Date()).format("YYYY-MM-DD-HH");
  if (sessionStorage.getItem(date) !== date) {
    arr = clone(obj);
    return { arr, status: "false" };
  }
  return { arr };
};

export default encryption;
export const a = (a) => {
  return a;
};
